package gui;
	
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

import client.Answers;
import client.Main;
import client.Survey;

public class Survey_GUI extends JFrame implements ActionListener{	
	
	
	//variables
	public static String backFrom=null;
	public static int flag=-1;
	public static Integer disributeFlag=-1; 
	public static Integer d=-1; 
	public Survey survey_control = new Survey();
	public Survey srv_control = new Survey();
	public static Integer[] s;
	public static Integer[] st;
	public String str;
	public static int privflag=-1;
	public static String sn[]=new String[2];
	public Integer ans[]={1,2,3,4,5,6,7,8,9,10};
	public Integer srvAns[]=new Integer[6];
	public Survey subAnalyze=null;
	public Survey srv=null;
	public Answers_GUI answers_gui = null;
	public static Integer feedflag=-1;
	//Jtable
	public JTable table;
	public JScrollPane ScrollPane = null; 
	
	//Jtext
	JTextArea textAnalyze=null;

	//panels
	public JPanel panelExpert= null;
	public JPanel panelExpert1= null;
	public JPanel panelExpert2= null;
	public JPanel panelService = null;
	public JPanel panelEmployee = null;
	public JPanel panelEmployee1 = null;
	public JPanel panelAnsSum=null;	
	public JPanel panelDispSurvey=null;
	
	//buttons
	public JButton dispSurvey=null;
	public JButton Back = null;
	public JButton analyze= null;
	public JButton feedAnswers=null;
	public JButton dispAnswers=null;
	public JButton distributeSurvey=null;
	public JButton submitAnswers=null;
	public JButton submitAnalyze=null;
	public JButton displayAnalyze=null;
	public JButton btnLogOut=null;
	
	
	//ComboBox
	public JComboBox addAnlBox = null;
	public JComboBox distributedSurvey = null;
	public JComboBox q1=null;
	public JComboBox q2=null;
	public JComboBox q3=null;
	public JComboBox q4=null;
	public JComboBox q5=null;
	public JComboBox q6=null;
	
	//Labels
	JLabel AnalyzeSurvey;    
	JLabel lblSelectSurvey;  
	JLabel SrvMngSurvey;    
	JLabel AnswersSurvey_i;
	JLabel labelQ1;
	JLabel labelQ2;
	JLabel labelQ3;
	JLabel labelQ4;
	JLabel labelQ5;
	JLabel labelQ6;
	JLabel qi;

	void init(int sflag)
	{
		privflag=sflag;
		backFrom="mainScreen";
		this.setSize(410,410);
		this.setTitle("Zer Lah");
		this.setLocation((int)Main.dim.getWidth()/3 - (int)this.getPreferredSize().getWidth()/3,(int)Main.dim.getHeight()/4 - (int)this.getPreferredSize().getHeight()/4);
		this.setResizable(false);	
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	
		if(sflag==1)	this.add(getPanelExpert());
		if(sflag==2)
		{
			this.add(getPanelEmployee());
		}
		if(sflag==3)	this.add(getPanelService());
		this.setEnabled(true);
		this.setVisible(true);
	}
	 
	JPanel getPanelDispSurvey()
	{
		panelDispSurvey=new JPanel();
		panelDispSurvey.setLayout(null);
		panelDispSurvey.add(getButtonBack());
		if(privflag==2)
		{
			sn[0]="readSurvey";
			Survey.readSurvey(sn);
		}
		panelDispSurvey.add(getLableAnswersSurvey_i());
		for(int i=0;i<6;i++)
		panelDispSurvey.add(getLableQi(i));
		panelDispSurvey.setVisible(true);
		panelDispSurvey.setEnabled(true);
		if(privflag==3)
		{
			panelDispSurvey.add(getButtonDispAnalyze());
			backFrom="panelDispSurvey3";
			panelService.setEnabled(false);
			panelService.setVisible(false);
		}
		if(privflag==2)
		{
			panelDispSurvey.add(getButtonDispAnalyze());
			backFrom="panelDispSurvey2";
			panelEmployee.setEnabled(false);
			panelEmployee.setVisible(false);
		}
		if(privflag==1)
		{
			panelDispSurvey.add(getButtonDispAnswers());
			 backFrom="panelDispSurvey1";
			panelExpert.setEnabled(false);
			panelExpert.setVisible(false);
		}
		return panelDispSurvey;
	}
	
	JPanel getPanelExpert()
	{
		panelExpert= new JPanel();
		panelExpert.setLayout(null);
		panelExpert.add(getDistributedSurveySname());
		panelExpert.add(getButtonBack());
		 backFrom="mainScreen";
		 panelExpert.add(getBtnLogOut());
		//panelExpert.add(getButtonDispAnswers());
		panelExpert.add(getButtonDispSurvey());
		panelExpert.add(getButtonAnalyze());
		panelExpert.add(getLableSurvey());
		panelExpert.add(getLableAnalyzeSurvey());
		panelExpert.setEnabled(true);
		panelExpert.setVisible(true);
		return panelExpert;
	}

	JPanel getPanelExpert2()
	{
		panelExpert2= new JPanel();
		panelExpert2.add(getLableEnterAnalyze());
		backFrom="panelExpert2";
		panelExpert2.add(getLableAnswersSurvey_i());
		panelExpert2.add(getTextAnalyze());
		panelExpert2.add(getButtonSubmitAnalyze());
		panelExpert2.add(getButtonBack());
		panelExpert2.setLayout(null);
		panelExpert2.setVisible(true);
		panelExpert.setVisible(false);
		return panelExpert2;
	}

	JPanel getPanelEmployee()
	{
		panelEmployee= new JPanel();
		panelEmployee.setLayout(null);
		panelEmployee.add(getButtonBack());
		panelEmployee.add(getBtnLogOut());
		panelEmployee.add(getButtonFeedAnswers());
		panelEmployee.add(getDistributedSurveySname());
		panelEmployee.add(getLableSurvey());
		panelEmployee.add(getButtonDispSurvey());
		panelEmployee.add(getLableSrvMng());
		backFrom="mainScreen";
		panelEmployee.setEnabled(true);
		panelEmployee.setVisible(true);
		return panelEmployee;
	}
	JPanel getPanelEmployee1()
	{
		panelEmployee1=new JPanel();
		panelEmployee1.setLayout(null);
		backFrom="panelEmployee1";
		panelEmployee1.add(getLableAnswersSurvey_i());
		panelEmployee1.add(getButtonBack());
		panelEmployee1.add(getQ1());
		panelEmployee1.add(getQ2());
		panelEmployee1.add(getQ3());
		panelEmployee1.add(getQ4());
		panelEmployee1.add(getQ5());
		panelEmployee1.add(getQ6());
		panelEmployee1.add(getLabelQ1());
		panelEmployee1.add(getLabelQ2());
		panelEmployee1.add(getLabelQ3());
		panelEmployee1.add(getLabelQ4());
		panelEmployee1.add(getLabelQ5());
		panelEmployee1.add(getLabelQ6());
		panelEmployee1.add(getButtonSubmitAnswers());
		panelEmployee1.setVisible(true);
		panelEmployee.setVisible(false);
		return panelEmployee1;
	}
	JPanel getPanelService()
	{
		panelService= new JPanel();
		panelService.setLayout(null);
		backFrom="mainScreen";
		panelService.add(getBtnLogOut());
		panelService.add(getButtonBack());
		panelService.add(getSurveySname());
		panelService.add(getButtonDispSurvey());
		panelService.add(getLableSurvey());
		panelService.add(getLableSrvMng());
		panelService.add(getButtonDistributeSurvey());
		panelService.setEnabled(true);
		panelService.setVisible(true);
		return panelService;
	}

	JTextArea getTextAnalyze()
	{
		textAnalyze= new JTextArea(60,1);
		textAnalyze.setBounds(30,160,300,150);
		return textAnalyze;
	}
	JLabel getLableAnswersSurvey_i()//Title everyone
	{
		AnswersSurvey_i=new JLabel();
		AnswersSurvey_i.setText(sn[0]+" : "+sn[1]);
		AnswersSurvey_i.setBounds(50, 70, 300, 40);
		AnswersSurvey_i.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		return AnswersSurvey_i;
	}

	JLabel getLableQi(int i)//Enter analyze
	{
		qi=new JLabel();
		int j=i*30;
		int y=120+j;//to place all 6 questions
		String q=new String();
		if((q=srv.getQ(i)).equals(null)) return null; 
		qi.setText("Question "+(i+1)+" : "+q);
		qi.setBounds(40, y, 300, 20);
		qi.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,15));
		return qi;
	}
	JLabel getLableEnterAnalyze()//Enter analyze
	{
		JLabel enterAnalyze=new JLabel();
		enterAnalyze.setText("Enter Analyze:");
		enterAnalyze.setBounds(30, 120, 300, 20);
		enterAnalyze.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,15));
		return enterAnalyze;
	}
	JLabel getLableAnalyzeSurvey()//Title analyze
	{
		AnalyzeSurvey=new JLabel();
		AnalyzeSurvey.setText("Analyze Survey");
		AnalyzeSurvey.setBounds(120, 70, 250, 40);
		AnalyzeSurvey.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		return AnalyzeSurvey;
	}
	
	JLabel getLableSrvMng()//Title service + employee
	{
		SrvMngSurvey=new JLabel();
		SrvMngSurvey.setText("Survey Managing");
		SrvMngSurvey.setBounds(120, 70, 250, 40);
		SrvMngSurvey.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		return SrvMngSurvey;
	}
	JLabel getLableSurvey()
	{
		lblSelectSurvey=new JLabel();
		lblSelectSurvey.setText("Select Survey :");
		lblSelectSurvey.setBounds(60,155, 250, 40);
		lblSelectSurvey.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		return lblSelectSurvey;
	}
	JComboBox getDistributedSurveySname()
	{
		distributedSurvey= new JComboBox();
		srv_control.get_DistributedSurveySid();
		for(int i=0;i<st.length;i++)
		{
			if(st[i]!=null)
				distributedSurvey.addItem("Survey_"+st[i]);//s[i]);
		}
		distributedSurvey.setBounds(240, 155, 100, 40);
		return distributedSurvey;
	}
	JComboBox getSurveySname()
	{
		addAnlBox= new JComboBox();
		survey_control.get_SurveySid();
		for(int i=0;i<s.length;i++)
		{
			if(s[i]!=null)
			addAnlBox.addItem("Survey_"+s[i]);//s[i]);
		}
		addAnlBox.setBounds(240, 155, 100, 40);
		return addAnlBox;
	}
	JComboBox getQ1()
	{
		q1=new JComboBox();
		for(int i=0;i<ans.length;i++)
			q1.addItem(ans[i]);
		q1.setBounds(250, 120, 70, 20);
		return q1;
	}
	JComboBox getQ2()
	{
		q2=new JComboBox();
		for(int i=0;i<ans.length;i++)
			q2.addItem(ans[i]);
		q2.setBounds(250, 150, 70, 20);
		return q2;
	}
	JComboBox getQ3()
	{
		q3=new JComboBox();
		for(int i=0;i<ans.length;i++)
			q3.addItem(ans[i]);
		q3.setBounds(250, 180, 70, 20);
		return q3;
	}
	JComboBox getQ4()
	{
		q4=new JComboBox();
		for(int i=0;i<ans.length;i++)
			q4.addItem(ans[i]);
		q4.setBounds(250, 210, 70, 20);
		return q4;
	}
	JComboBox getQ5()
	{
		q5=new JComboBox();
		for(int i=0;i<ans.length;i++)
			q5.addItem(ans[i]);
		q5.setBounds(250, 240, 70, 20);
		return q5;
	}
	JComboBox getQ6()
	{
		q6=new JComboBox();
		for(int i=0;i<ans.length;i++)
			q6.addItem(ans[i]);
		q6.setBounds(250, 270, 70, 20);
		return q6;
	}
	JLabel getLabelQ1()
	{
		labelQ1=new JLabel();
		labelQ1.setText("Question - 1:");
		labelQ1.setBounds(110, 120, 110, 20);
		return labelQ1;
	}
	JLabel getLabelQ2()
	{
		labelQ2=new JLabel();
		labelQ2.setText("Question - 2:");
		labelQ2.setBounds(110, 150, 110, 20);
		return labelQ2;
	}
	JLabel getLabelQ3()
	{
		labelQ3=new JLabel();
		labelQ3.setText("Question - 3:");
		labelQ3.setBounds(110, 180,110, 20);
		return labelQ3;
	}
	JLabel getLabelQ4()
	{
		labelQ4=new JLabel();
		labelQ4.setText("Question - 4:");
		labelQ4.setBounds(110, 210, 110, 20);
		return labelQ4;
	}
	JLabel getLabelQ5()
	{
		labelQ5=new JLabel();
		labelQ5.setText("Question - 5:");
		labelQ5.setBounds(110, 240, 110, 20);
		return labelQ5;
	}
	JLabel getLabelQ6()
	{
		labelQ6=new JLabel();
		labelQ6.setText("Question - 6:");
		labelQ6.setBounds(110, 270, 110, 20);
		return labelQ6;
	}
	JButton getButtonDispAnalyze()
	{
		displayAnalyze=new JButton();
		displayAnalyze.setText("Display Survey Analyze");
		displayAnalyze.setBounds(80, 310, 220, 40);
		displayAnalyze.addActionListener(this);
		return displayAnalyze;
	}
	
		JButton getBtnLogOut() 
		{
		if (btnLogOut == null){
			btnLogOut = new JButton();
			btnLogOut.setBounds(20,20,100,40);
			btnLogOut.addActionListener(this);
			btnLogOut.setText("LogOut");
		}
		return btnLogOut;
	}

	JButton getButtonAnalyze()
	{
		analyze=new JButton();
		analyze.setText("Analyze");
		analyze.setBounds(60,240,280,40);
		analyze.addActionListener(this);
		return analyze;
	}
	JButton getButtonSubmitAnalyze()
	{
		submitAnalyze=new JButton();
		submitAnalyze.setText("Submit Analyze");
		submitAnalyze.setBounds(80,330,200,40);
		submitAnalyze.addActionListener(this);
		return submitAnalyze;
	}
	JButton getButtonSubmitAnswers()
	{
		submitAnswers=new JButton();
		submitAnswers.setText("Submit Answers");
		submitAnswers.setBounds(80,300,280,40);
		submitAnswers.addActionListener(this);
		return submitAnswers;
	}
	JButton getButtonDispAnswers()
	{
		dispAnswers=new JButton();
		dispAnswers.setText("Display Survey Answers");
		dispAnswers.setBounds(60,300,280,40);
		dispAnswers.addActionListener(this);
		return dispAnswers;
	}
	JButton getButtonDispSurvey()
	{
		dispSurvey=new JButton();
		dispSurvey.setText("Display Survey ");
		dispSurvey.setBounds(60,300,280,40);
		dispSurvey.addActionListener(this);
		return dispSurvey;
	}
	JButton getButtonFeedAnswers()
	{
		feedAnswers=new JButton();
		feedAnswers.setText("Feed Survey Answers");
		feedAnswers.setBounds(60,240,280,40);
		feedAnswers.addActionListener(this);
		return feedAnswers;
	}
	JButton getButtonDistributeSurvey()
	{
		distributeSurvey=new JButton();
		distributeSurvey.setText("Distribute Survey ");
		distributeSurvey.setBounds(60,240,280,40);
		distributeSurvey.addActionListener(this);
		return distributeSurvey;
	}
 
	JButton getButtonBack()
	{
		if(Back==null)
		{
			Back=new JButton();
			Back.setText("Back");
			Back.setBounds(290,10,100,40);
			Back.addActionListener(this);
		}
		return Back;
	}

	public Survey_GUI(int sflag) 
	{		
		super();
		init(sflag);	
	}
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnLogOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				this.setEnabled(false);
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
		if(e.getSource()==analyze)
		{
			sn[1]=(String) distributedSurvey.getSelectedItem();
			sn[0]="Input Analyze";
			this.add(getPanelExpert2());
		}
		if(e.getSource()==dispAnswers)
		{
			sn[0]="Display Answers";
			sn[1]=(String) distributedSurvey.getSelectedItem();
			String astr[]=new String[2];
			astr=sn[1].split("_");
			String str=sn[1];
			sn[1]=astr[1];
			Answers.getSurveyAnswers(sn);
			sn[1]=str;
			answers_gui=new Answers_GUI();
		}
		if(e.getSource()==feedAnswers)
		{
			sn[0]="Feed Answers";
			sn[1]=(String) distributedSurvey.getSelectedItem();
			sn[1]=(String) distributedSurvey.getSelectedItem();
			this.add(getPanelEmployee1());
		}
		if(e.getSource()==submitAnswers)
		{
			sn[1]=(String) distributedSurvey.getSelectedItem();//////////
			String split[]=new String[2];
			split=sn[1].split("_");
			int aInt = Integer.parseInt(split[1]);
			srvAns[0]= (Integer) q1.getSelectedItem();
			srvAns[1]= (Integer) q2.getSelectedItem();
			srvAns[2]= (Integer) q3.getSelectedItem();
			srvAns[3]= (Integer) q4.getSelectedItem();
			srvAns[4]= (Integer) q5.getSelectedItem();
			srvAns[5]= (Integer) q6.getSelectedItem();	
			new Answers(aInt,srvAns);
			if (feedflag==0){JOptionPane.showMessageDialog(null,"Sorry: Your Answers Aren't Saved! Try again...","Feed Answers Error",JOptionPane.ERROR_MESSAGE);}
			else
			{
				if(feedflag==1)
				{
					JOptionPane.showMessageDialog(null,"youre answers are saved successfully!!!\n"," Feed Answers "+sn[1],JOptionPane.DEFAULT_OPTION);
				}
			}
		}
		if(e.getSource()==submitAnalyze)
		{
			sn[1]=(String) distributedSurvey.getSelectedItem();
			String split[]=new String[2];
			split=sn[1].split("_");
			int sid=(Integer) Integer.parseInt(split[1]);
			subAnalyze=new Survey(sid);
			String str=textAnalyze.getText();
			if(str.isEmpty())
				JOptionPane.showMessageDialog(null,"Enter analyze before Submit","Analyze Eror",JOptionPane.ERROR_MESSAGE);
			else
			{
				subAnalyze.setAnalyse(str);	
			    flag=subAnalyze.save_SurveyAnalyze();
				if(flag==1)
					JOptionPane.showMessageDialog(null,"Analyze saved successfully");
				else
					JOptionPane.showMessageDialog(null,"Error in DB - try again","Analyze Eror1",JOptionPane.ERROR_MESSAGE);
			}
		}
		if(e.getSource()==dispSurvey)
		{
			sn[0]="Display Survey";
			if(privflag==3)		sn[1]=(String) addAnlBox.getSelectedItem();
			else 				sn[1]=(String) distributedSurvey.getSelectedItem();
			String split[]=new String[2];
			String str=sn[1];
			
			split=sn[1].split("_");
			sn[1]=split[1];
			srv=Survey.dispSurvey(sn);
			sn[1]=str;
			if(srv==null)
				JOptionPane.showMessageDialog(null,"Error in DB - try again","Survey managing Eror",JOptionPane.ERROR_MESSAGE);
			else
			{
				this.add(getPanelDispSurvey());
			}
		}
		if(e.getSource()==distributeSurvey)
		{
			sn[0]="Get Distribute";
			sn[1]=(String) addAnlBox.getSelectedItem();
			String split[]=new String[2];
			String str=sn[1];
			split=sn[1].split("_");
			sn[1]=split[1];
			Survey.getDistribute(sn);
			if(d==0)
			{
				sn[0]="Distribute Survey";
				Survey.distribute(sn);
				sn[1]=str;
				if(disributeFlag==0)
					JOptionPane.showMessageDialog(null,"Error in DB - try again later",sn[1]+" Distribute Eror",JOptionPane.ERROR_MESSAGE);
				else
				{
					if(disributeFlag==1)
						JOptionPane.showMessageDialog(null,sn[1]+" Is Distributed Succesfully!"," Distribute "+sn[1],JOptionPane.DEFAULT_OPTION);
				}
			}
			else
			{
				if(d==1)
					JOptionPane.showMessageDialog(null,"Survey "+sn[1]+" Is Allready Distributed !"," Distribute Survey "+sn[1],JOptionPane.DEFAULT_OPTION);
			}
		}
		if(e.getSource()==displayAnalyze)
		{
			int f=srv.check_analyze();
			if(f==0){JOptionPane.showMessageDialog(null,"No Analyze To Display!",sn[1]+" Analyze",JOptionPane.DEFAULT_OPTION);}
			else 
			{
				String anal=srv.getAnalyze();
				JOptionPane.showMessageDialog(null,"Analyze:\n"+anal,sn[1]+" Analyze",JOptionPane.DEFAULT_OPTION);
			}
		}
		if(e.getSource()== Back)
		{
			if(backFrom.equals("mainScreen"))
			{
				this.setEnabled(false);
				this.setVisible(false);
				Main.main.showMainScreen();
			}
			if(backFrom.equals("panelExpert2"))
			{ 
				panelExpert2.setEnabled(false);
				panelExpert2.setVisible(false);
				this.add(getPanelExpert());
			}
			if(backFrom.equals("panelDispSurvey1"))
			{ 
				panelDispSurvey.setEnabled(false);
				panelDispSurvey.setVisible(false);
				this.add(getPanelExpert());
			}
			if(backFrom.equals("panelDispSurvey2"))
			{ 
				panelDispSurvey.setEnabled(false);
				panelDispSurvey.setVisible(false);
				this.add(getPanelEmployee());
			}
			if(backFrom.equals("panelDispSurvey3"))
			{ 
				panelDispSurvey.setEnabled(false);
				panelDispSurvey.setVisible(false);
				this.add(getPanelService());
			}
			if(backFrom.equals("panelEmployee1"))
			{ 
				panelEmployee1.setEnabled(false);
				panelEmployee1.setVisible(false);
				this.add(getPanelEmployee());
			}
		}		
	}		
}

	
	
